<?php
// 本类由系统自动生成，仅供测试用途
class CarrierAction extends CommonAction {
	public function _before_index() {
		$model = M ( "Carrier" );
		$list = $model->getField ( 'id,carriername,contactor,phone,fax,site,email,address,zip,openbank,account,notes' );
		$this->assign ( 'groupList', $list );
	}
	public function read(){
		$model = M ( "Carrier" );
		$id=$_REQUEST['id'];
		$vo=$model->where('id='.$id)->find();
		$this->assign ( 'vo', $vo );
		$this->display();
	}
	public function _before_add() {
		$model = M ( "Carrier" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function _before_foreverdelete(){
		$ids = $_REQUEST['ids'];
		$model = M('Freight');
		$id = explode(',', $ids);
		if($id!==null){
			for ($i=0;$i<count($id);$i++){
				$model->where('carrierid='.$id[$i])->delete();
			}
		}
	}
	public function _before_edit() {
		$model = M ( "Carrier" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function _before_insert(){
		$model = M('Carrier');
		$Carrierdata = $model->getByCarriername($_POST ['carriername']);
		if ($Carrierdata) {
			$this->ajaxReturn ( '0', '该 承运商名称 已经存在!', '300' );
		} 
	}
	public function _before_update(){
		$model = M('Carrier');
		$id = $_REQUEST['id'];
		$Carrierdata = $model->getByCarriername($_POST ['carriername']);
		if($Carrierdata){
			if ($Carrierdata['id']!==$id) {
				$this->ajaxReturn ( '0', '该 承运商名称 已经存在!', '300' );
			}
		}
	}
	/**
	 * 结算
	 */
	public function pay()
	{
		$model = M ( "Carrier" );
		//承运商的id
		$id=$_REQUEST['id'];
		$carrierdetail=$model->where('id='.$id)->find();
		//$payid=$_REQUEST['payid'];
		$carriername=$carrierdetail['carriername'];
		$model1=M("CarrierDispatch");
		$list=$model1->where('carrierid=\''.$id.'\' and status=0')->order('id desc')->select();
		$this->assign('list',$list);
		/* $model2=M('CarrierPayment');
		$map['collection']=$carriername;
		$map['openbank']=$carrierdetail['openbank'];
		$map['account']=$carrierdetail['account'];
		$map['totalment']=0;
		$map['carrierid']=$id; */
		//if($model2->create())
		//{
		/* if(empty($payid)){
			$result=$model2->data($map)->add();
			if($result)
			{
				//保存的付款的id
				$this->assign('payid',$result);
				$totalment=0;
				
				//$this->ajaxReturn (1,'添加成功!',200);
				
			}
		}
		else 
		{ */
			//$this->assign('payid',$payid);
			$res=$model->where('id='.$id)->find();
			$totalment=$res['totalment'];
			$payer=$res['payer'];
		     $payperson=$res['payperson'];
		     $getunit=$res['getunit'];
		     $paytime=$res['paytime'];
		     $notes=$res['notes'];
		     
			//$carriername=$res['carriername'];
		//}
		 $this->assign('payer',$payer);
		 $this->assign('getunit',$getunit);
		 $this->assign('payperson',$payperson);
		 $this->assign('notes',$notes);
		 $this->assign('paytime',$paytime);
		 $this->assign('totalment',$totalment);
         $this->assign('collection',$carriername);
         $this->assign('openbank',$carrierdetail['openbank']);
         $this->assign('account',$carrierdetail['account']);
			//承运商的id
			$this->assign('carrierid',$id);
			$this->display();
		//}
		
		
	}
		
	/**
	 * 点击计算
	 */
	public function checked()
	{
		$ids=$_REQUEST['ids'];
		//付款信息的id
		$id=$_REQUEST['id'];
		//承运商的id
		$payment=M('CarrierPayment');
		$pay=$payment->where('id='.$id)->find();
		$carrierid=$pay['carrierid'];
		$model=M("CarrierDispatch");
		$dispatch=$model->where('id='.$ids)->find();
		$start=$dispatch['beginning'];
		$end=$dispatch['destination'];
		$cweight=$dispatch['cweight'];
		$model1=M('Freight');
		$map['startaddress']=$start;
		$map['endaddress']=$end;
		$map['carrierid']=$carrierid;
		$lists=$model1->where($map)->select();
		if(!empty($lists)){
			 $price=$lists[0]['unitprice'];
			 $freight=$price*$cweight;
			 $data['freight']=$freight;
			 $data['status']=1;
			 $result= $model->where('id='.$ids)->save($data);
			 if($result)
			 {
			 	//$this->pay();
			 	$totalment=$pay['totalment']+$freight;
			 	$datas['totalment']=$totalment;
			 	//$datas['payer']=$_REQUEST['payer'];
			 	$payment->where('id='.$id)->save($datas);
			 	//$this->assign('totalment',$totalment);
			 	//$this->assign('id',$id);
			 	//$this->assign('carrierid',$carrierid);
			 	$this->ajaxReturn (1,'结算成功!',200);
			 }
			 else
			 {
			 	$this->ajaxReturn (1,'结算失败!',300);
			 }
		}
		else 
		{
			$this->ajaxReturn(1,'没有两地之间的运费价格!',300);
			
		}
		
	}
	/**
	 * 保存付款信息
	 */
	public function insertPay(){
		  $model=M('CarrierDispatch');
		  $model1=M('CarrierPayment');
	      $id = $_REQUEST ['checkboxx'];
	      //$ids=$_REQUEST['payid'];
	      $totalment=$_POST['totalment'];
	      $data['carrierid']=$_POST['carrierid'];
	      $data['openbank']=$_POST['openbank'];
	      $data['payer']=$_POST['payer'];
	      $data['payperson']=$_POST['payperson'];
	      $data['collection']=$_POST['collection'];
	      $data['getunit']=$_POST['getunit'];
	      $data['account']=$_POST['account'];
	      $data['collection']=$_POST['collection'];
	      $data['paytime']=$_POST['paytime'];
	      $data['notes']=$_POST['notes'];
	      $data['totalment']=$totalment;
	      $result = $model1->add($data);//付款记录
	      if ($result!==false) {
	      	for($i=0;$i< count($id);$i++)
	      	{
	      	$datas['status']=1;
	      			$result=$model->where('id='.$id[$i])->save($datas);
	      	}
	      	if ($result!==false) { 
	      	$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
	     	$this->ajaxReturn (1,'结算成功!',200);
	      	} else {
	      	$this->ajaxReturn (1,'结算失败!',300);
	      	}
	      } else {
	      	$this->ajaxReturn (1,'结算失败!',300);
	      }
	}
	
	/**
	 * 查看付款信息
	 */
	public function readpay()
	{
		$id=$_REQUEST['id'];
		$model=M('CarrierPayment');
		$currentpage=$_POST['pageNum'];//当前页数
		if(empty($currentpage))
		{
			$currentpage=1;
		}
		$pagenum=$_POST['numPerPage'];//每页显示的条数
		if(empty ($pagenum))
		{
			$pagenum=20;
		}
		$count=$model->where('carrierid='.$id)->count();
		$list=$model->where('carrierid='.$id)->page($currentpage,$pagenum)->select();
		
		$modelCarrier=M('Carrier');
		$carrierBank=$modelCarrier->where('id='.$id)->select();
		$this->assign('openbank',$carrierBank[0]['openbank']);
		$this->assign('account',$carrierBank[0]['account']);
		
		$this->assign('list',$list);
		$this->assign('totalCount',$count);
		$this->assign('currentPage',$currentpage);
		$this->assign('id',$id);
		$this->assign("numPerPage",$pagenum);		
		$this->display();		
	}
	
	//双击查看单条付款信息
	public function viewapay()
	{
		$id=$_REQUEST['id'];
		$model=M('CarrierPayment');
		$vo=$model->where('id='.$id)->find();	
		$this->assign('vo',$vo);
		$this->display();
	}
	
	public function _before_car(){
		$cus = M ( "Customer" );
		$Customer=$cus->select ();		
		$model2 = M ( "Provider" );
		$Provider=$model2->select();
		$this->assign ( 'customer', $Customer );
		$this->assign ( 'provider', $Provider );
	}
	
	/**
	 * 查看租车信息
	 */
	public function car()
	{
		$id=$_REQUEST['id'];
		$dispatch=M('CarrierDispatch');
		import ( '@.Org.Page' );
		$count = $dispatch->where('carrierid=\''.$id.'\'')->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 20 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $dispatch->where('carrierid=\''.$id.'\'')->page ( $pageNum . "," . $page->listRows )->order('id desc')->select ();
		$this->assign ( 'id', $id );
		$this->assign ( 'list', $list );
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	
	/**
	 * 编辑租车信息
	 *
	 */
	
	public function editcar()
	{
		$id=$_REQUEST['id'];
		$dispatch=M('CarrierDispatch');
		$result=$dispatch->where('id='.$id)->find();
		$this->assign('vo',$result);
		$this->display();
		
	}
	/**
	 * 更新租车信息
	 * 
	 */
	public function updatecar()
	{
		$id=$_REQUEST['id'];
		$dispatch=M('CarrierDispatch');
		if($dispatch->create())
		{
			$result=$dispatch->where('id='.$id)->save();
			if($result)
			{
				$this->ajaxReturn($result,'更新成功',200);
			}
			else
			{
				$this->ajaxReturn($result,'更新失败',300);
			}
		}
		else
		{
			$this->error($dispatch->getError());
		}
		
	}
	
	/**
	 * 运费输入
	 */
	public function freight(){
		$model = M ( "Carrier" );
		$model1 = M('Freight');
		$id=$_REQUEST['id'];
		//dump($id);
		import ( '@.Org.Page' );
		$count = $model1->where('carrierid=\''.$id.'\'')->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 20 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $model1->where('carrierid=\''.$id.'\'')->page ( $pageNum . "," . $page->listRows )->select ();
		$this->assign ( 'id', $id );
		$this->assign ( 'list', $list );
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display();
	}
	/**
	 * 增加运费信息
	 */
	public function freightadd(){
		$model = M ( "Carrier" );
		$id=$_REQUEST['id'];
		$this->assign('id',$id);
		$this->display();
	}
	
	/**
	 * 保存新添加运费信息
	 */
	public function insertfreight(){
		$model=M('Freight');
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		//保存当前数据对象
		$list=$model->add ();
		if ($list!==false) { //保存成功
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->ajaxReturn (1,'新增成功!',200);
		} else {
			//失败提示
			$this->ajaxReturn (1,'新增失败!',300);
		}
	}
	/**
	 * 修改运费信息
	 */
	public function editfreight()
	{
		$id=$_REQUEST['id'];
		$Freight=M('Freight');
		$result=$Freight->where('id='.$id)->find();
		$this->assign('vo',$result);
		$this->display();
	}
	/**
	 * 更新运费信息
	 */
	public function updatefreight()
	{
		$id=$_REQUEST['id'];
		$Freight=M('Freight');
		if($Freight->create())
		{
			$result=$Freight->where('id='.$id)->save();
			if($result)
			{
				$this->ajaxReturn($result,'更新成功',200);
			}
			else
			{
				$this->ajaxReturn($result,'更新失败',300);
			}
		}
		else
		{
			$this->error($Freight->getError());
		}
	
	}
	/**
	 * 删除运费价格记录
	 */
	public function freightdelete(){
		
		$id = $_REQUEST['ids'];
		//dump($id);
		
		$Freight = M('Freight');
		$condition = array (id => array ('in', explode ( ',', $id ) ) );
		$result = $Freight->where($condition)->delete();
			if($result)
			{
				$this->ajaxReturn($result,'删除成功',200);
			}
			else
			{
				$this->ajaxReturn($result,'删除失败',300);
			}
	}
	
	/**
	 * 删除租车记录
	 */
	public function cardelete(){
	
		$id = $_REQUEST['ids'];
	
		$CarrierDispatch = M('CarrierDispatch');
		$condition = array (id => array ('in', explode ( ',', $id ) ) );
		$result = $CarrierDispatch->where($condition)->delete();
		if($result)
		{
			$this->ajaxReturn($result,'删除成功',200);
		}
		else
		{
			$this->ajaxReturn($result,'删除失败',300);
		}
	}
	/**
	 * 导出excel
	 */
	
	public function export(){
	
		$model=D('Carrier');
	
	
	
		$carriername=$_REQUEST['carriername'];
		if(!empty($carriername)){
			$map['carriername']=array('like',"%" . $carriername . "%");
		}
	
		$Contract=$model->where($map)->field('')->select();
		$cname=date('Y-m-d');
			
		$map = array('承运商名称','联系人','电话','传真','网址','邮箱','地址','邮编','开户银行','银行账户','备注');
		$field=array('carriername','contactor','phone','fax','site','email','address','zip','openbank','account','notes');
			
		$this->phpexcel($Contract, $cname, $map,$field);
	}
			
}